package im.dhc.gitdiff.common;

import im.dhc.gitdiff.util.FileUtil;
import im.dhc.gitdiff.util.PathUtil;
import im.dhc.gitdiff.util.PropertityUtil;
import org.eclipse.jgit.diff.DiffEntry;

import java.io.File;

/**
 * @author songhaichao
 * @create 2020-07-13-10:55
 **/
public class DiffFile {

    private String fileName;
    private String path;
    private String changeType;

    public DiffFile (){

    }
    public DiffFile(DiffEntry diff){
        this.changeType = diff.getChangeType().name();
        if("ADD".equals(changeType) || "MODIFY".equals(changeType) || "RENAME".equals(changeType)){
            this.path = PathUtil.getPath(diff.getNewPath());
            this.fileName = path == null? null : this.path.substring(this.path.lastIndexOf("/")+1);
        }else{
            this.path = null;
            this.fileName = null;
        }
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public void copyFile() throws Exception {
        PropertityUtil PropertityUtil = new PropertityUtil();
        String projectName = PropertityUtil.getProperty("projectName");
        String onlineDate = PropertityUtil.getProperty("onlineDate");
        String localTargetDir = PropertityUtil.getProperty("localTargetDir");
        String localUpdateDir = PropertityUtil.getProperty("localUpdateDir");
        String sourcePath = localTargetDir+"/"+path;
        String targetPath = localUpdateDir+"/"+onlineDate+"/"+projectName+"/"+fileName;

        File sourceFile = new File(sourcePath);
        FileUtil.makeDirs(targetPath.substring(0,targetPath.lastIndexOf("/")));
        File targetFile = new File(targetPath);

        FileUtil.copyFile(sourceFile,targetFile);
    }
}
